package com.ity.teamanagement.service.Impl;

import com.ity.teamanagement.code.CommonCode;
import com.ity.teamanagement.code.DownNavigationCode;
import com.ity.teamanagement.code.HomeModuleCode;
import com.ity.teamanagement.code.SysConfigCode;
import com.ity.teamanagement.entity.DownNavigation;
import com.ity.teamanagement.entity.HomeModule;
import com.ity.teamanagement.entity.SysUrl;
import com.ity.teamanagement.entity.Systems;
import com.ity.teamanagement.entity.inputParam.*;
import com.ity.teamanagement.entity.outputResult.DownNavigationVo;
import com.ity.teamanagement.entity.outputResult.HomeModuleVo;
import com.ity.teamanagement.entity.outputResult.QrCodeList;
import com.ity.teamanagement.entity.outputResult.TeaMessageList;
import com.ity.teamanagement.exception.ExceptionUtils;
import com.ity.teamanagement.mapper.*;
import com.ity.teamanagement.result.Result;
import com.ity.teamanagement.service.QrCodeService;
import com.ity.teamanagement.service.SystemConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author coderFu
 * @create 2021/6/5
 */
@Service
public class SystemConfigServiceImpl implements SystemConfigService {

    @Resource
    private SystemConfigMapper systemConfigMapper;

    @Resource
    private QrCodeMapper qrCodeMapper;

    @Resource
    private QrCodeService qrCodeService;

    @Resource
    private TeaManagementMapper teaManagementMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Autowired
    private HomeModuleMapper homeModuleMapper;

    @Autowired
    private DownNavigationMapper downNavigationMapper;


    @Override
    public void findQrCodeUrl() {
        String url = systemConfigMapper.findQrCodeUrl();
        if (url == null || "".equals(url)){
            ExceptionUtils.error(CommonCode.DATA_ERROR);
        }
        redisTemplate.opsForValue().set("URL",url,30L,TimeUnit.DAYS);
    }

    @Override
    public List<SysUrl> findAllUrlConfig() {
        return systemConfigMapper.findAllUrlConfig();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUrlConfigById(SysUrlParam sysUrlParam) {
        SysUrl sysUrl =  new SysUrl();
        sysUrl.setId(sysUrlParam.getId());
        sysUrl.setUrl(sysUrlParam.getUrl());
        sysUrl.setUpdateTime(new Date());
        Integer count = systemConfigMapper.updateUrlConfigById(sysUrl);
        if (count<1){
            ExceptionUtils.error(SysConfigCode.URL_UPDATE_FAIL);
        }
        findQrCodeUrl();
        List<QrCodeList> qrCodeLists = qrCodeMapper.backFindAllTeaQrCode();
        List<Long> imgIds = qrCodeLists.stream().map(QrCodeList::getId).collect(Collectors.toList());
        qrCodeMapper.deleteTeaQrCodeByIds(imgIds);
        qrCodeMapper.deleteAllQrCode();
        List<TeaMessageList> teaMessageLists = teaManagementMapper.findAllTeaMessageIds();
        List<Long> teaMessageIds = teaMessageLists.stream().map(TeaMessageList::getId).collect(Collectors.toList());
        qrCodeService.produceOneQrCodeById(teaMessageIds);
    }

    @Override
    public void deleteUrlConfigByIds(List<Long> ids) {
        if (ids.size() == 0){
            ExceptionUtils.error(CommonCode.PARAMETER_ERROR);
        }
        Integer count = systemConfigMapper.deleteUrlConfigByIds(ids);
        if (count<1){
            ExceptionUtils.error(SysConfigCode.URL_DELETE_FAIL);
        }
    }

    @Override
    public void addQrCodeUrl(SysUrlParam sysUrlParam) {
        if(sysUrlParam == null){
            ExceptionUtils.error(CommonCode.PARAMETER_ERROR);
        }
        SysUrl sysUrl = new SysUrl();
        sysUrl.setUrl(sysUrlParam.getUrl());
        sysUrl.setState(0L);
        sysUrl.setCreateTime(new Date());
        Integer count = systemConfigMapper.addQrCodeUrl(sysUrl);
        if (count<1){
            ExceptionUtils.error(SysConfigCode.URL_ADD_FAIL);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void settingQrCodeUrl(Long id) {
        Integer count = systemConfigMapper.settingQrCodeUrl(id);
        //将其它的状态改为未使用的状态
        Integer otherCount = systemConfigMapper.updateOtherUrl(id);
        if (otherCount<0){
            ExceptionUtils.error(SysConfigCode.URL_SET_ERROR);
        }
        if (count<1){
            ExceptionUtils.error(SysConfigCode.URL_SET_ERROR);
        }
        //将redis中的二维码路径信息更新为最新的
        findQrCodeUrl();
        System.out.println();
        List<QrCodeList> qrCodeLists = qrCodeMapper.backFindAllTeaQrCode();
        List<Long> imgIds = qrCodeLists.stream().map(QrCodeList::getId).collect(Collectors.toList());
        qrCodeMapper.deleteTeaQrCodeByIds(imgIds);
        qrCodeMapper.deleteAllQrCode();
        List<TeaMessageList> teaMessageLists = teaManagementMapper.findAllTeaMessageIds();
        List<Long> teaMessageIds = teaMessageLists.stream().map(TeaMessageList::getId).collect(Collectors.toList());
        qrCodeService.produceOneQrCodeById(teaMessageIds);
    }

    @Override
    public Systems checkOpenStatus() {
        return systemConfigMapper.findSystemOpenStatus();
    }

    @Override
    public void closeReceptionShow() {
        Integer integer = systemConfigMapper.closeReceptionShow();
        if (integer<1){
            ExceptionUtils.error(SysConfigCode.RECEPTION_CLOSE_FAIL);
        }
    }

    @Override
    public void openReceptionShow() {
        Integer integer = systemConfigMapper.openReceptionShow();
        if (integer<1){
            ExceptionUtils.error(SysConfigCode.RECEPTION_OPEN_FAIL);
        }
    }

    @Override
    public Result addHomeModule(AddHomeModuleParam addHomeModuleParam) {
            //接入外部模块
            HomeModule homeModule = new HomeModule();
            homeModule.setName(addHomeModuleParam.getName());
            homeModule.setLink(addHomeModuleParam.getLink());
            homeModule.setCreateTime(new Date());
            Integer integer = homeModuleMapper.addHomeModule(homeModule);
            if (integer <1){
                ExceptionUtils.error(HomeModuleCode.HOME_MODULE_ADD_FAIL);
            }
            return Result.success();
    }

    @Override
    public Result deleteHomeModule(DeleteHomeModuleParam deleteHomeModuleParam) {
        Integer integer = homeModuleMapper.deleteHomeModule(deleteHomeModuleParam.getId());
        if (integer <1){
            ExceptionUtils.error(HomeModuleCode.HOME_MODULE_DELETE_FAIL);
        }
        return Result.success();
    }

    @Override
    public Result updateHomeModule(UpdateHomeModuleParam updateHomeModuleParam) {
        //根据id判断是否属于不可改变的模块 1 新闻 2 学术研究
        HomeModule homeModule = new HomeModule();
        if (updateHomeModuleParam.getId() == 1 || updateHomeModuleParam.getId() == 2){
            //只能修改名称不能添加超链接
            homeModule.setName(updateHomeModuleParam.getName());
            homeModule.setId(updateHomeModuleParam.getId());
        }else {
            //可以修改两个内容
            homeModule.setId(updateHomeModuleParam.getId());
            homeModule.setName(updateHomeModuleParam.getName());
            homeModule.setLink(updateHomeModuleParam.getLink());
        }
        homeModule.setUpdateTime(new Date());
        //存储到数据库
        Integer integer = homeModuleMapper.updateHomeModule(homeModule);
        if (integer <1){
            ExceptionUtils.error(HomeModuleCode.HOME_MODULE_UPDATE_FAIL);
        }
        return Result.success();
    }

    @Override
    public Result getHomeModule() {
        List<HomeModuleVo> homeModuleVoList = homeModuleMapper.getHomeModule();
        return Result.success(homeModuleVoList);
    }

    @Override
    public Result addDownNavigation(AddDownNavigationParam addDownNavigationParam) {
        DownNavigation downNavigation = new DownNavigation();
        BeanUtils.copyProperties(addDownNavigationParam,downNavigation);
        downNavigation.setCreateTime(new Date());
        Integer integer = downNavigationMapper.addDownNavigation(downNavigation);
        if (integer <1){
            ExceptionUtils.error(DownNavigationCode.DOWN_NAVIGATION_ADD_FAIL);
        }
        return Result.success();
    }

    @Override
    public Result deleteDownNavigation(DeleteDownNavigationParam deleteDownNavigationParam) {
        Integer integer = downNavigationMapper.deleteDownNavigation(deleteDownNavigationParam.getId());
        if (integer <1){
            ExceptionUtils.error(DownNavigationCode.DOWN_NAVIGATION_DELETE_FAIL);
        }
        return Result.success();
    }

    @Override
    public Result updateDownNavigation(UpdateDownNavigationParam updateDownNavigationParam) {
        DownNavigation downNavigation = new DownNavigation();
        BeanUtils.copyProperties(updateDownNavigationParam,downNavigation);
        downNavigation.setUpdateTime(new Date());
        Integer integer = downNavigationMapper.updateDownNavigation(downNavigation);
        if (integer <1){
            ExceptionUtils.error(DownNavigationCode.DOWN_NAVIGATION_UPDATE_FAIL);
        }
        return Result.success();
    }

    @Override
    public Result getDownNavigation() {
        List<DownNavigationVo> downNavigationVos = downNavigationMapper.getDownNavigation();
        return Result.success(downNavigationVos);
    }

    @Override
    public Result getDownNavigationById(SelectDownNavigationParam selectDownNavigationParam) {
        DownNavigation downNavigation = downNavigationMapper.getDownNavigationById(selectDownNavigationParam.getId());
        return Result.success(downNavigation);
    }

    @Override
    public Result getHomeModuleById(SelectHomeModuleParam selectHomeModuleParam) {
        HomeModule homeModule = homeModuleMapper.getHomeModuleById(selectHomeModuleParam.getId());
        return Result.success(homeModule);
    }
}
